package cn.tedu.ivos2.dict.service.impl;

import cn.tedu.ivos2.dict.mapper.DictMapper;
import cn.tedu.ivos2.dict.pojo.dto.DictQuery;
import cn.tedu.ivos2.dict.pojo.dto.DictSaveDTO;
import cn.tedu.ivos2.dict.pojo.entity.Dict;
import cn.tedu.ivos2.dict.pojo.vo.DictVO;
import cn.tedu.ivos2.dict.service.DictService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class DictServiceImpl implements DictService {
    @Autowired
    private DictMapper dictMapper;

    @Override
    public List<DictVO> selectDic(DictQuery dictQuery) {
        log.debug("S1-查询字典条件:{}",dictQuery);
        List<DictVO> dictList=dictMapper.query(dictQuery);
        log.debug("S2-查询字典结果:{}",dictList);
        return dictList;
    }

    @Override
    public void saveDict(DictSaveDTO dictSaveDTO) {
        Dict dict=new Dict();
        BeanUtils.copyProperties(dictSaveDTO,dict);
        if (dictSaveDTO.getId()==null){
            dict.setCreateTime(new Date());
            log.debug("S1-保存字典数据:{}",dict);
            dictMapper.save(dict);
        }else {
            log.debug("S1-更新字典:{}",dict);
            dictMapper.update(dict);
        }

    }

    @Override
    public void deleteDic(Long id) {
        log.debug("S1-删除字典的id{}",id);
        dictMapper.del(id);
    }
}
